<?php
echo "<pre>";
print_r($_POST);
echo "</pre>";

//	ADD USER INFORMATION
if(isset($_POST[cmdSubmitAddStudent]) && $_POST[cmdSubmitAddStudent]!='')
{
$data['si_clientId'] = $_SESSION[clientID];
$data['si_firstName'] = $_POST[txtFirstName];
$data['si_lastName'] = $_POST[txtLastName];
$data['si_standard'] = $_POST[txtStandard];
$data['si_section'] = $_POST[txtSection];
$data['si_rollNo'] = $_POST[txtRollNo];
$data['si_RFIDNo'] = $_POST[txtRFIDNo];
$data['si_fatherName'] = $_POST[txtFatherName];
$data['si_emailId'] = $_POST[txtEmailId];
$data['si_mobile1'] = $_POST[txtPrimaryMobile];
$data['si_mobile2'] = $_POST[txtAlterMobile];
$data['si_pickup_id'] = $_POST[txtRoutePickupId]."#".$_POST[sourcePt];
$data['si_drop_id'] = $_POST[txtRouteDropId]."#".$_POST[destPt];
$data['si_address'] = $_POST[txtStudAddress];
print_r($data);
	

	//exit;
	$primary_id = $db->query_insert("tb_student_info", $data);
	
	if($primary_id)
	{
		$sourceId = $primary_id."#";		
		$getDeviceGeofence = "SELECT * FROM tb_geofence_info WHERE tgi_id = ".$_POST[sourcePt]." AND tgi_stud_pick_ids like '%".$sourceId."%' AND tgi_clientId = ".$_SESSION[clientID];	
		$resDeviceGeofence = $db->query($getDeviceGeofence);
		if($db->affected_rows == 0 )
		{
			$geodata['tgi_stud_pick_ids'] = $sourceId;
			if($db->query_update("tb_geofence_info", $geodata , "tgi_id=".$_POST[sourcePt]))
			$src = 1;
		}
		else
		{
			$getDeviceGeo = $db->fetch_array($resDeviceGeofence);
			$pos = strrpos($geodata['tgi_stud_pick_ids'], $sourceId);
			if ($pos === false) 
			{ 
				$geodata['tgi_stud_pick_ids'] = $geodata['tgi_stud_pick_ids'].$sourceId;
				if($db->query_update("tb_geofence_info", $geodata , "tgi_id=".$_POST[sourcePt]))
				$src = 1;
			}else $src = 0;
		}
		
		$destId = $primary_id."#";
		$getDeviceGeofence = "SELECT * FROM tb_geofence_info WHERE tgi_id = ".$_POST[destPt]." AND tgi_stud_drop_ids like '%".$destId."%' AND tgi_clientId = ".$_SESSION[clientID];	
		$resDeviceGeofence = $db->query($getDeviceGeofence);
		if($db->affected_rows == 0 )
		{
			$geodata['tgi_stud_drop_ids'] = $destId;
			if($db->query_update("tb_geofence_info", $geodata , "tgi_id=".$_POST[destPt]))
			$dst = 1;
		}
		else
		{
			$getDeviceGeo = $db->fetch_array($resDeviceGeofence);
			$pos = strrpos($geodata['tgi_stud_drop_ids'], $sourceId);
			if ($pos === false) 
			{ 
				$geodata['tgi_stud_drop_ids'] = $geodata['tgi_stud_drop_ids'].$destId;
				if($db->query_update("tb_geofence_info", $geodata , "tgi_id=".$_POST[destPt]))
				$dst = 1;
			}else $dst = 0;
		}
	}
	if($src == 1 && $dst == 1)
		$res = 1;
	else $res =0;
//exit;
header("location:?ch=status&au=24&msg=".$res);
exit;

}


//		UPDATE USER INFORMATION
if(isset($_POST[cmdSubmitUpdateStudent]) && $_POST[cmdSubmitUpdateStudent]!='')
{
	$data['si_firstName'] = $_POST[txtFirstName];
	$data['si_lastName'] = $_POST[txtLastName];
	$data['si_standard'] = $_POST[txtStandard];
	$data['si_section'] = $_POST[txtSection];
	$data['si_rollNo'] = $_POST[txtRollNo];
	$data['si_RFIDNo'] = $_POST[txtRFIDNo];
	$data['si_fatherName'] = $_POST[txtFatherName];
	$data['si_emailId'] = $_POST[txtEmailId];
	$data['si_mobile1'] = $_POST[txtPrimaryMobile];
	$data['si_mobile2'] = $_POST[txtAlterMobile];
	$data['si_pickup_id'] = $_POST[txtRoutePickupId]."#".$_POST[sourcePt];
	$data['si_drop_id'] = $_POST[txtRouteDropId]."#".$_POST[destPt];
	$data['si_address'] = $_POST[txtStudAddress];
	
	print_r($data);
	echo "<br>";
	if($db->query_update("tb_student_info", $data,"si_id=".$_POST[txtStudId]))
	{
	
		$sourceId = $_POST[txtStudId]."#";		
		$q1 = "SELECT * FROM tb_geofence_info WHERE tgi_id = ".$_POST[txtPickUp]." AND tgi_stud_pick_ids like '%".$sourceId."%' AND tgi_clientId = ".$_SESSION[clientID];
		$r1 = $db->query($q1);
		if($db->affected_rows > 0 )
		{			
			$f1 = $db->fetch_array($r1);
			$srcdata['tgi_stud_pick_ids'] = str_replace($sourceId,"",$f1[tgi_stud_pick_ids]);
			$db->query_update("tb_geofence_info", $srcdata , "tgi_id=".$_POST[txtPickUp]);
		}
		
		$getDeviceGeofence = "SELECT * FROM tb_geofence_info WHERE tgi_id = ".$_POST[sourcePt]." AND (tgi_stud_pick_ids like '%".$sourceId."%' or tgi_stud_pick_ids !='' ) AND tgi_clientId = ".$_SESSION[clientID];	
		$resDeviceGeofence = $db->query($getDeviceGeofence);
		if($db->affected_rows == 0 )
		{
			$geodata1['tgi_stud_pick_ids'] = $sourceId;
			if($db->query_update("tb_geofence_info", $geodata1 , "tgi_id=".$_POST[sourcePt]))
			$src = 1;
		}
		else
		{
			$getDeviceGeo = $db->fetch_array($resDeviceGeofence);
			$pos = strrpos($getDeviceGeo['tgi_stud_pick_ids'], $sourceId);
			if ($pos === false) 
			{ 
				$geodata1['tgi_stud_pick_ids'] = $getDeviceGeo['tgi_stud_pick_ids'].$sourceId;
				if($db->query_update("tb_geofence_info", $geodata1 , "tgi_id=".$_POST[sourcePt]))
				$src = 1;
			}else $src = 0;
		}
		
		$destId = $_POST[txtStudId]."#";
		$q2 = "SELECT * FROM tb_geofence_info WHERE tgi_id = ".$_POST[txtDrop]." AND tgi_stud_drop_ids like '%".$destId."%' AND tgi_clientId = ".$_SESSION[clientID];
		$r2 = $db->query($q2);
		if($db->affected_rows > 0 )
		{			
			$f2 = $db->fetch_array($r2);
			$desdata['tgi_stud_drop_ids'] = str_replace($destId,"",$f2[tgi_stud_drop_ids]);
			$db->query_update("tb_geofence_info", $desdata , "tgi_id=".$_POST[txtDrop]);
		}
		
		$getDeviceGeofence = "SELECT * FROM tb_geofence_info WHERE tgi_id = ".$_POST[destPt]." AND (tgi_stud_drop_ids like '%".$destId."%' or tgi_stud_drop_ids !='') AND tgi_clientId = ".$_SESSION[clientID];	
		$resDeviceGeofence = $db->query($getDeviceGeofence);
		if($db->affected_rows == 0 )
		{
			$geodata2['tgi_stud_drop_ids'] = $destId;
			if($db->query_update("tb_geofence_info", $geodata2 , "tgi_id=".$_POST[destPt]))
			$dst = 1;
		}
		else
		{
			$getDeviceGeo = $db->fetch_array($resDeviceGeofence);
			
			$pos = strrpos($getDeviceGeo['tgi_stud_drop_ids'], $destId);
			if ($pos === false) 
			{ 
				$geodata2['tgi_stud_drop_ids'] = $getDeviceGeo['tgi_stud_drop_ids'].$destId;
				if($db->query_update("tb_geofence_info", $geodata2 , "tgi_id=".$_POST[destPt]))
				$dst = 1;
			}else $dst = 0;
		}
	}
	
	if($src == 1 && $dst == 1)
		$res = 1;
	else $res =0;
//exit;	
header("location:?ch=status&au=25&msg=".$res);
exit;

}


?>